<?php

namespace CNEPAdmin\Model;

use Think\Model;

class PrivModuleModel extends Model {

    protected $_validate = array(/* array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]) */
        array('privName', 'require', '名称不能为空'),
        array('privCode', 'require', '标识不能为空'),
        array('privCode', '', '该标识已存在', 0, 'unique'),
    );  // 自动验证定义
    protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
        array('addTime', 'time', 1, 'function'),
        array('updateTime', 'time', 2, 'function'),
    );  // 自动完成定义

    public function addModule($data = '') {
        $rt = false;

        if ($this->create($data)) {
            $this->startTrans();

            $rt = $this->add();

            if ($rt) {
                if (I("post.pid") > 0) { // pid的is_leaf => 0
                    $data = array();
                    $data['privID'] = I("post.pid");
                    $data['isLeaf'] = 0;
                    $data['updateTime'] = time();

                    if (!$this->data($data)->save()) {
                        $this->rollback();
                        $this->error = "保存失败" . $this->getDbError() . $this->getError();
                        return false;
                    }
                }
                $this->commit();
            } else {
                $this->rollback();
                $this->error = "保存失败" . $this->getDbError() . $this->getError();
                return false;
            }
        }
        return $rt;
    }

    /**
     * 更新
     * @param type $adminID
     * @param type $key
     * @param type $val
     */
    public function updateMsg($data = '') {
        $rt = false;

        if ($this->create($data)) {
            $this->startTrans();

            $rt = $this->save();

            if ($rt) {
                if (I("post.pid") > 0) { // pid的is_leaf => 0
                    $data = array();
                    $data['privID'] = I("post.pid");
                    $data['isLeaf'] = 0;
                    $data['updateTime'] = time();

                    if (!$this->data($data)->save()) {
                        $this->rollback();
                        $this->error = "更新父id" . $this->getDbError() . $this->getError();
                        return false;
                    }
                }
                $this->commit();
            } else {
                $this->rollback();
                $this->error = "保存失败" . $this->getDbError() . $this->getError();
                return false;
            }
        }
        return $rt;
    }

    public function deleteModule($id) {
        return $this->where("privID=$id")->delete();
    }

    public function getModule($id) {
        return $this->where("privID=$id")->find();
    }

    public function getModuleList($pageNo = 1, $pageSize = 15, $where = array(), $order = array()) {
        $rt = $this->where($where)->page("$pageNo,$pageSize")->order($order)->select();
        return $rt;
    }

    public function getModuleListLength($where = array()) {
        return $this->where($where)->count();
    }

    //////////////系统权限管理////////////////////////
    /**
     * 同步角色权限到memcache中
     */
    public function sychronRolePriv() {

        $keyList = D("SysRoleModule")->field("priv,memcacheKey")->select();

        if ($keyList) {
            foreach ($keyList as $val) {
                S($val['memcacheKey'], $val['priv']);
            }
        }
    }

    /**
     * 得到pid = 0 的大模块用于显示
     */
    public function getModuleListForMenu() {
        return $this->where(array("pid" => 0))->select();
    }

}

?>